<template>
  <a-modal
    :title="model && model.id > 0 ? '编辑会员' : '新增会员'"
    :width="640"
    :visible="visible"
    :confirmLoading="loading"
    @ok="
      () => {
        $emit('ok')
      }
    "
    @cancel="
      () => {
        $emit('cancel')
      }
    "
  >
    <a-spin :spinning="loading">
      <a-form :form="form" v-bind="formLayout">
        <a-form-item label="id" v-show="false">
          <a-input v-decorator="['id']" />
        </a-form-item>
        <a-form-item label="姓名">
          <a-input v-decorator="['name', { rules: [{ required: true, message: '请输入金额' }] }]" placeholder="请输入姓名" />
        </a-form-item>
        <a-form-item label="电话">
          <a-input
            v-decorator="[
              'phone',
              {
                initialValue: model?.phone,
                rules: [
                  { pattern: /^1[3456789]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' },
                ],
              },
            ]"
            placeholder="请输入电话"
          />
        </a-form-item>
        <a-form-item label="会员等级">
          <a-select v-decorator="['level', { initialValue: '1' }]" placeholder="请选择">
            <a-select-option value="0">普卡</a-select-option>
            <a-select-option value="1">VIP</a-select-option>
          </a-select>
        </a-form-item>
        <!-- <a-form-item label="店面">
          <a-select v-decorator="['storeId', { initialValue: model?.storeId }]" placeholder="请选择" >
            <a-select-option :value="i.id" v-for="i in storeoption" :key="i.id">{{ i.storeName }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="销售员" >
          <a-select v-decorator="['empId', { initialValue: model?.empId }]" placeholder="请选择">
            <a-select-option :value="i.id" v-for="i in useroption" :key="i.id">{{ i.userName }}</a-select-option>
          </a-select>
        </a-form-item> -->
        <a-form-item label="卡号">
          <a-input v-decorator="['cardNo', { initialValue: model?.cardNo }]" placeholder="请输入卡号" />
        </a-form-item>
        <a-form-item label="加入日期">
          <a-date-picker v-decorator="['joinDate', { initialValue:this.toDays}]" valueFormat="YYYY-MM-DD" />
        </a-form-item>
        <a-form-item label="身份证号">
          <a-input
            v-decorator="[
              'idCardNo',
              {
                initialValue: model?.idCardNo,
                rules: [
                  {
                    pattern: /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/,
                    message: '请输入正确的身份证',
                    trigger: 'blur',
                  },
                ],
              },
            ]"
            placeholder="请输身份证号"
          />
        </a-form-item>
        <a-form-item label="性别">
          <a-select v-decorator="['sex', { initialValue: model?.sex }]" placeholder="请选择">
            <a-select-option value="1">男</a-select-option>
            <a-select-option value="2">女</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="出生日期">
          <a-date-picker v-decorator="['birthday', { initialValue: model?.birthday }]" valueFormat="YYYY-MM-DD" />
        </a-form-item>
        <a-form-item v-if="model && model.id > 0" label="销售负责人">
          <a-select v-decorator="['empId', { initialValue: model?.empId }]" placeholder="请选择">
            <a-select-option :value="i.id" v-for="i in userOption" :key="i.id">{{ i.userName }}</a-select-option>
          </a-select>
        </a-form-item>
        <!-- <a-form-item v-if="model && model.id > 0"  label="标签">
          <a-select mode="multiple" v-decorator="['labelId', { initialValue: model?.mark }]" placeholder="请选择">
            <a-select-option :value="i.id" v-for="i in markOption" :key="i.id">{{ i.name }}</a-select-option>
          </a-select>
        </a-form-item> -->
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { getEmployeesListUsingPOST } from '@/api/api/yuangongguanli.ts'
import { getStoreListUsingPOST } from '@/api/api/mendianguanli.ts'
import {getMemLabelListUsingPOST} from '@/api/api/xitongguanli.ts'
import pick from 'lodash.pick'
import moment from 'moment'

// 表单字段
const fields = ['name', 'phone', 'level', 'cardNo', 'joinDate', 'idCardNo', 'sex', 'birthday', 'multiple','labelId', 'id']

export default {
  props: {
    visible: {
      type: Boolean,
      required: true,
    },
    loading: {
      type: Boolean,
      default: () => false,
    },
    model: {
      type: Object,
      default: () => null,
    },
  },
  data() {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 },
      },
    }
    return {
      form: this.$form.createForm(this),
      optionParam: {
        pageNo: 1,
        pageSize: 999,
        queryCondition: '',
        status: 1,
      },
      userOption:[],
      toDays: moment().format('YYYY-MM-DD'),
      storeoption: [],
      markOption: [],
    }
  },
  created() {
    // 防止表单未注册
    this.getUserOption()
    this.getStoreOption()
    this.getMarkOption()
    fields.forEach((v) => this.form.getFieldDecorator(v))

    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      this.model && this.form.setFieldsValue(pick(this.model, fields))
    })
  },
  methods: {
    getMarkOption(){
      getMemLabelListUsingPOST(this.optionParam)
        .then((res) => {
          this.markOption = res.data.list
        })
        .catch((error) => {
          this.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
        })
    },
    
    getStoreOption() {
      getStoreListUsingPOST(this.optionParam)
        .then((res) => {
          this.storeoption = res.data.list
        })
        .catch((error) => {
          this.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
        })
    },
    getUserOption() {
      getEmployeesListUsingPOST(this.optionParam)
        .then((res) => {
          this.userOption = res.data.list
        })
        .catch((error) => {
          this.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
        })
    },
  },
}
</script>
